package com.zerowidth.db.op;

import com.zerowidth.db.DBHelper;
import com.zerowidth.db.base.SqlResult;
import com.zerowidth.db.bean.AppConfigBean;
import com.zerowidth.db.utils.IOUtils;
import com.zerowidth.moc.web.utils.StringUtils;
import jakarta.servlet.http.HttpServletRequest;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AppConfigDB {

    public static List<AppConfigBean> getAppConfigData(String app_channel) {
        return getAppConfigData(app_channel, null);
    }

    public static List<AppConfigBean> getAppConfigData(String app_channel, HttpServletRequest request) {

//        String versionName = request.getHeader("appVersionName");
//        String appVersionCode = request.getHeader("appVersionCode");
//        String appName = request.getHeader("appName");

        String sql = String.format("SELECT * FROM app_config WHERE channel = '%s' OR channel IS NULL", app_channel);

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            if (sr == null) {
                return null;
            }
            ResultSet rs = sr.rs;
            List<AppConfigBean> configList = new ArrayList<>();
            while (rs.next()) {
                AppConfigBean bean = new AppConfigBean();
                bean.id = rs.getInt("id");
                bean.config_key = rs.getString("config_key");
                bean.config_value = rs.getString("config_value");

                configList.add(bean);
            }

            return configList;
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }

}
